
var obj = {
	id:'1',
	pid:'0',
	name:'',
	sendobj:'',
	sendobjChild:[],
	sendtime:'',
	sendtimeChild:'',
	sendmethod:[],
	rules:'',
	num:0,//人数
	children:[]
};//此对象需要上传到服务器
var currentObj = null;//当前节点对象

function showInfo(){
	currentObj = obj;
	$('#infoName').text($('#activeName').text());	
	$('input:checkbox,input:radio').removeAttr("checked");
	$('input[name="sendtimeDate"]').val('');
	if(currentObj.sendobj){
		$('input:radio[name="sendobj"][value="'+currentObj.sendobj+'"]').prop('checked',true);
	}
	if(currentObj.sendobjChild.length>0){
		for(var i = 0; i < currentObj.sendobjChild.length; i ++){
			$('input:checkbox[name="sendobj'+currentObj.sendobj+'"][value="'+currentObj.sendobjChild[i]+'"]').prop('checked',true);
		}
	}
	if(currentObj.sendtime){
		$('input:radio[name="sendtime"][value="'+currentObj.sendtime+'"]').prop('checked',true);
	}
	if(currentObj.sendtimeChild){
		$('input:radio[name="sendtime"][value="'+currentObj.sendtime+'"] ~ input').val(currentObj.sendtimeChild);
	}
	if(currentObj.sendmethod.length > 0){
		for(var i = 0; i < currentObj.sendmethod.length; i ++){
			$('input:checkbox[name="sendmethod"][value="'+currentObj.sendmethod[i]+'"]').prop('checked',true);
		}
	}
	if(currentObj.rules){
		$('input:radio[name="rules"][value="'+currentObj.rules+'"]').prop('checked',true);
	}
}

var nodes = {
	nameObj:null,
	pid:'',
	text:'1', //区分同级元素
	node:function(){
		return '<td colspan="2" id="td_'+nodes.pid+'"><table>'+
			'<tr id="tr1_'+nodes.pid+'" data-num="'+nodes.text+'">'+
				'<td colspan="0">'+
					'<div class="box flex-direction width">'+
						'<div class="box item bac" style="display:block;" id="item_'+nodes.pid+'">'+
							'<div class="con-top"><a href="javascript:void(0);" class="a-top" onclick="nodes.showModel(this)">选择活动</a></div>'+
							'<div class="item-bottom con-bottom">'+
								'<p class="con-left a-init"><span>0</span>人</p>'+
								'<div class="con-right"><a class="a-init" href="javascript:void(0);" onclick="nodes.addNode(\''+nodes.pid+'\')">添加下级活动</a></div>'+
							'</div>'+
							'<div class="remove" onclick="nodes.removeNode(\''+nodes.pid+'\')">-</div>'+
						'</div>'+
					'</div>'+
				'</td>'+
			'</tr>'+
			'<tr class="lines" id="tr2_'+nodes.pid+'">'+
			'</tr>'+
			'<tr class="lines" id="tr3_'+nodes.pid+'">'+
			'</tr>'+
			'<tr class="nodes" id="tr4_'+nodes.pid+'">'+
			'</tr>'+
		'</table></td>';
	},
	addLine:function(id){  // 连接线
		var length = $('#tr4_'+id+'>td').length * 2;
		$('#tr1_'+id+'>td').attr('colspan',length);
		if(length === 0){
			$('#tr2_'+id).html('');
			$('#tr3_'+id).html('');
			return;
		} 
		var downline = '<td colspan="'+length+'"><div class="downLine"></div></td>';
		$('#tr2_'+id).html(downline);
		var topline = '';
		var rightline = '<td class="rightLine">&nbsp;</td>';
		var leftline = '<td class="leftLine">&nbsp;</td>';
		var line = '';
		if(length/2 - 1 > 0){
			for (var i = 0; i < length/2 -1; i++) {
				topline += '<td class="leftLine topLine">&nbsp;</td><td class="rightLine topLine">&nbsp;</td>';
			}
		}
		line = rightline + topline + leftline;
		$('#tr3_'+id).html(line);
	},
	counttree:function(data){
		var c = "_";// 要计算的字符
		var regex = new RegExp(c, 'g'); // 使用g表示整个字符串都要匹配
		var result = data.match(regex);
		var count = !result ? 0 : result.length;
		return count;
	},
	addNode:function(id){ //添加节点
		if(currentObj&& currentObj.children.length && currentObj.children.length === 3) return;
		this.text = $('#tr1_'+id).data('num');
		this.pid = id+'_'+$('#tr1_'+id).data('num');
		$('#tr4_'+id).append(this.node);
		this.addLine(id);
		$('#tr1_'+id).data('num',parseInt(this.text)+1);
		if(this.counttree(this.pid) == 1){
			$('#item_' + this.pid).css('background','#03a9f4');
		}else if(this.counttree(this.pid) == 2){
			$('#item_' + this.pid).css('background','#fd7167');
		}

		//添加属性值
		this.addPrototype(id);
		
	},
	removeNode:function(id){ //删除节点
		var parid = $('#td_'+id).closest('tr')[0].id.replace('tr4_','');
		$('#td_'+id).remove();
		this.addLine(parid);
		this.removeElemnt(id,obj);
	},
	addPrototype:function(id){//添加属性对象
		var nodeObj = {
			id:this.pid,
			pid:id,
			name:'',
			sendobj:'',
			sendobjChild:[],
			sendtime:'',
			sendtimeChild:'',
			sendmethod:[],
			rules:'',
			num:0,
			children:[]
		};
		this.findElement(id,obj,nodeObj);
		$('#item_'+this.pid).on('click',function(e){
			var id = this.id.replace('item_','');
			nodes.findNode(id,obj);
			var name = currentObj.name ? currentObj.name : '还未选择活动名';
			$('#infoName').text(name);

			$('input:checkbox,input:radio').removeAttr("checked");
			$('input[name="sendtimeDate"]').val('');
			if(currentObj.sendobj){
				$('input:radio[name="sendobj"][value="'+currentObj.sendobj+'"]').prop('checked',true);
			}
			if(currentObj.sendobjChild.length>0){
				for(var i = 0; i < currentObj.sendobjChild.length; i ++){
					$('input:checkbox[name="sendobj'+currentObj.sendobj+'"][value="'+currentObj.sendobjChild[i]+'"]').prop('checked',true);
				}
			}
			if(currentObj.sendtime){
				$('input:radio[name="sendtime"][value="'+currentObj.sendtime+'"]').prop('checked',true);
			}
			if(currentObj.sendtimeChild){
				$('input:radio[name="sendtime"][value="'+currentObj.sendtime+'"] ~ input').val(currentObj.sendtimeChild);
			}
			if(currentObj.sendmethod.length > 0){
				for(var i = 0; i < currentObj.sendmethod.length; i ++){
					$('input:checkbox[name="sendmethod"][value="'+currentObj.sendmethod[i]+'"]').prop('checked',true);
				}
			}
			if(currentObj.rules){
				$('input:radio[name="rules"][value="'+currentObj.rules+'"]').prop('checked',true);
			}
			console.log(obj);
		});
	},
	findNode:function(id,node){
		var nodeChild = node.children;
		for(var i = 0; i < nodeChild.length; i ++){
			if(id == nodeChild[i].id){
				currentObj = nodeChild[i];
				break;
			}else{
				this.findNode(id,nodeChild[i]);
			}
		}
	},
	findElement:function(pid,node,sonNode){
		if(pid == node.id){
			node.children.push(sonNode);
		}else{
			var nodeChild = node.children;
			for(var i = 0; i < nodeChild.length; i ++){
				this.findElement(pid,nodeChild[i],sonNode);
			}
		}
	},
	removeElemnt:function(id,node){
		var nodeChild = node.children;
		for(var i = 0; i < nodeChild.length; i ++){
			if(id == nodeChild[i].id){
				nodeChild.splice(i,1);
				break;
			}else{
				this.removeElemnt(id,nodeChild[i]);
			}
		}
	},
	showModel:function(e){
		$('.s_cancel').click(function(){
            $('.s_popup').hide();
            $('.issue').hide();
        });
        $('.s_cancels').click(function(){
            $('.s_popups').hide();
            $('.s_number_list').hide();
        });

        $('.s_popup .issue > span').click(function(){
            $('.s_popup').hide();
            $('.issue').hide();
        });
         $('.s_popups .s_number_list > span').click(function(){
            $('.s_popups').hide();
            $('.s_number_list').hide();
        });
        
        $('.s_popups').show();
        $('.s_number_list').show();
        nameActive = '';
        this.nameObj = e;
        console.log($(e).text());
	}
};

function addName(){

	if(nameActive){
		$(nodes.nameObj).text(nameActive);
		currentObj.name = nameActive;
		$(nodes.nameObj).removeClass('a-top');
		$(nodes.nameObj).closest('div').parent('.item').removeClass('bac');
		$(nodes.nameObj).closest('div').next().children('p').removeClass('a-init');
	}

	
	$('.s_popups').hide();
    $('.s_number_list').hide();
}

function submitInfo(){ //提交信息
	var sendobj = $('input:radio[name="sendobj"]:checked').val();
	var sendobjChild = [];
	$('input:checkbox[name="sendobj'+sendobj+'"]:checked').each(function(){
		sendobjChild.push(this.value);
	});

	var sendtime = $('input:radio[name="sendtime"]:checked').val();
	if($('input:radio[name="sendtime"]:checked ~ input').length > 0){
		var sendtimeChild = $('input:radio[name="sendtime"]:checked ~ input').val();
	}

	var sendmethod = [];
	$('input:checkbox[name="sendmethod"]:checked').each(function(){
		sendmethod.push(this.value);
	});
	var rules = $('input:radio[name="rules"]:checked').val();
	if(currentObj){
		currentObj.sendobj = sendobj;
		currentObj.sendobjChild = sendobjChild;
		currentObj.sendtime = sendtime;
		currentObj.sendmethod = sendmethod;
		currentObj.rules = rules;
		if(sendtimeChild){
			currentObj.sendtimeChild = sendtimeChild;
		}else{
			currentObj.sendtimeChild = '';
		}
	}
	console.log(obj);
}
var nameActive = '';
// 选择model
function chooseActive(){
	var target = event.target;
	if(target.src){
		$('.s_fk_pic1').each(function(e){
			this.src = 'images/s_check_no.png';
		});
		target.src = 'images/s_check.png';
	}
	nameActive = $(target).closest('td').next().text();
}





